package com.hp.bon.sgw.wh.mml.parser;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;

import org.dom4j.Document;

import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.util.Comutils;
import com.hp.bon.sgw.wh.mml.MMLEncoder;

public class XmlMessageParser {
	
	public static XmlMessage parseWSRequest(XMLInputFactory inputFactory, String representation) throws Exception {
		InputStream input = new ByteArrayInputStream(representation.getBytes("UTF-8"));
		XMLStreamReader xmlStreamReader = inputFactory.createXMLStreamReader(input);
		
		XmlMessage req = new XmlMessage(true);
		req.setLength(representation.getBytes().length);
		
		int nextType = 0;
		boolean seenSgwQueryReq = false;
		while (xmlStreamReader.hasNext()) {
			nextType = xmlStreamReader.next();
			if (nextType == XMLStreamReader.START_ELEMENT) {
				String elName = xmlStreamReader.getLocalName();
				if (elName.equals("SgwQueryReq")) {
					seenSgwQueryReq = true;
				} else if (seenSgwQueryReq) {
					String value = xmlStreamReader.getElementText();
					// System.out.println(elName + ":" + value);
					if (elName.equals("MID")) {
						req.setMID(value);
					} else if (elName.equals("SCID")) {
						req.setSCID(value);
					} else if (elName.equals("UID")) {
						req.setUID(value);
					} else if (elName.equals("PWD")) {
						req.setPWD(value);
					} else if (elName.equals("SIG")) {
						req.setSIG(value);
					}  else if (elName.equals("REPTAG")) {
						req.setREPTAG(Integer.parseInt(value.trim()));
					}else if(elName.equals("RSV1")){
						req.setRSV1(value);
					} else if(elName.equals("RSV2")){
						req.setRSV2(value);							
					} else if (elName.equals("DATA")) {
						Document doc = Comutils.parseText(value);
						if(doc!=null && doc.getRootElement()!=null){
							req.setDATA(MMLEncoder.buildMMLTransactionCommandData(doc.getRootElement()));							
						}
					} 
				}
			}
		}

		return req;
	}
	
	/******************************
	 * 从SoapChannel中提取出，经
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public static XmlMessage parseWSResponse(XMLInputFactory inputFactory, String response) throws Exception {
		InputStream input = new ByteArrayInputStream(response.getBytes("UTF-8"));
		XMLStreamReader xmlStreamReader = inputFactory.createXMLStreamReader(input);
		XmlMessage ack = new XmlMessage(false);
		int nextType = 0;

		boolean seenSgwQueryAck = false;
		while (xmlStreamReader.hasNext()) {
			nextType = xmlStreamReader.next();
			if (nextType == XMLStreamReader.START_ELEMENT) {
				String elName = xmlStreamReader.getLocalName();
				if (elName.equals("SgwQueryAck")) {
					seenSgwQueryAck = true;
				} else if (seenSgwQueryAck) {
					String value = xmlStreamReader.getElementText();
					if (elName.equals("MID")) {
						ack.setMID(value);
					} else if (elName.equals("RTT")) {
						ack.setRTT(value);
					} else if (elName.equals("RC")) {
						ack.setRC(value);
					} else if (elName.equals("SIG")) {
						ack.setSIG(value);
					} else if (elName.equals("DATA")) {
						ack.setDATA(value);
					} else if (elName.equals("DS")) {
						ack.setDS(value);
					}
				}
			}
		}

		if (ack.getDATA() != null && !"".equals(ack.getDATA())) {
			String data = ack.getDATA();
			Document doc = Comutils.parseText(data);
			ack.setDataElement(doc.getRootElement());
		}
		return ack;
	}

}
